<?php

/**
 * 推文model
 */
namespace backend\models;

use Yii;
use yii\db\ActiveRecord;
use yii\data\Pagination;
use yii\data\ArrayDataProvider;

class Tweet extends ActiveRecord
{
    /**
     * 获取推文列表
     */
    public function getTweetListByChannel( $params = array() )
    {
        $result = array();
        $limit = $params['pageSize'];
        $count = 0;
        if(isset($params['channel_id']) && intval($params['channel_id'])){
            $model = self::find();
            $model->select('*');
            if(isset($params['name']) && trim($params['name']) != ''){
                $model->andWhere('novel_name like :name');
                $model->addParams([':name'=>'%'.trim($params['name']).'%']);
            }
            $page = $params['page'];
            $offset = 0;
            if($page > 0){
                $offset = ($page - 1) * $limit;
            }
            $model->andWhere('channel_id = :id');
            $model->addParams([':id'=>intval($params['channel_id'])]);
            $count = $model->count();
            $model->offset( $offset )->limit( $limit );
            $result = $model->asArray()->all();
        }
        $data['dataProvider'] = new ArrayDataProvider(array(
            'allModels'=> $result,
//            'sort' => [
//                'attributes' => ['userId', 'createTime', 'sex', 'nickname','lastTime'],
//            ],
        ));
        $data['pagination'] = new Pagination(array(
            'defaultPageSize'   => $limit,
            'totalCount'        => $count,
        ));
        return $data;
    }
    
    /**
     * 根据id获取
     */
    public static function getById($id)
    {
        if(!$id){
            return array();
        }
        $model = self::find();
        $model->select("*");
        $model->andWhere(['id'=>$id]);
        $data = $model->asArray()->one();
        $keyword = TweetKeyword::getByTweetId($id);
        $data['kw'] = isset($keyword['name']) ? $keyword['name'] : '';
        return $data;
    }
    
    /**
     * 充值
     */
    public static function getChargeSum($sn)
    {
        $sql_user = "select user_id from xs_user where user_reg_recommend_sn= :sn";
        $user = Yii::$app->db->createCommand($sql_user)->bindValue(':sn',$sn)->queryAll();
        if($user){
            $ids = implode(',',array_column($user,'user_id'));
            $sql = "select SUM(pay_money) as money,count(id) as cnt from xs_recharge where user_id in ({$ids}) and status=1";
            $data = Yii::$app->db->createCommand($sql)->queryOne();
        }else{
            $data = array(
                'money' => 0,
                'cnt' => 0,
            );
        }
        return $data;
    }
    
    /**
     * 充值
     */
    public static function getConsumeSum($sn)
    {
        $sql_user = "select user_id from xs_user where user_reg_recommend_sn= :sn";
        $user = Yii::$app->db->createCommand($sql_user)->bindValue(':sn',$sn)->queryAll();
        if($user){
            $ids = implode(',',array_column($user,'user_id'));
            $sql = "select SUM(pay_coin) as money,count(id) as cnt from xs_user_consume where user_id in ({$ids}) and status=1";
            $data = Yii::$app->db->createCommand($sql)->queryOne();
        }else{
            $data = array(
                'money' => 0,
                'cnt' => 0,
            );
        }
        return $data;
    }
    
}

